package com.pretty.adminapp.configure;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.session.data.redis.config.ConfigureRedisAction;

import java.io.Serializable;
import java.lang.reflect.Method;

/**
 * 类描述：redis和ehcache
 * 创建者：袁越
 * 创建日期：2017/4/2
 * 创建时间：13:40
 * Copyright:版权
 * © 2016-2020 上海金扳手科技信息股份有限公司 All rights reserved.
 */
@Configuration
@EnableCaching
public class CachingConfig extends CachingConfigurerSupport {
    @Bean
    public KeyGenerator biliKeyGenerator() {
        return new KeyGenerator() {

            @Override
            public Object generate(Object target, Method method, Object... params) {
                StringBuilder sb = new StringBuilder();
                sb.append(target.getClass().getName());
                sb.append(method.getName());
                for (Object obj : params) {
                    sb.append(obj.toString());
                }
                return sb.toString();
            }
        };

    }

    /**
     * 连接Redis
     *
     * @return
     */
    @ConditionalOnMissingBean
    @Bean("redisConnectionFactory")
    public JedisConnectionFactory redisConnectionFactory() {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        // host地址
        jedisConnectionFactory.setHostName("127.0.0.1");
        // 端口号
        jedisConnectionFactory.setPort(6379);
        jedisConnectionFactory.setPassword("12345678");
        jedisConnectionFactory.afterPropertiesSet();
        return jedisConnectionFactory;
    }

    /**
     * RedisTemplate配置
     *
     * @param redisCF
     * @return
     */
    @ConditionalOnMissingBean
    @Bean("redisTemplate")
    public RedisTemplate<Serializable, Serializable> redisTemplate(
            @Qualifier(value = "redisConnectionFactory") RedisConnectionFactory redisCF) {
        RedisTemplate<Serializable, Serializable> redisTemplate = new RedisTemplate<Serializable, Serializable>();
        redisTemplate.setConnectionFactory(redisCF);
        //定义value的序列化方式
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    /**
     * 缓存管理器
     *
     * @param redisTemplate
     * @return
     */
    @Primary
    @Bean("cacheManager")
    public CacheManager cacheManager(RedisTemplate<Serializable, Serializable> redisTemplate) {
        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
        //设置过期时间
        cacheManager.setDefaultExpiration(3000);
        return cacheManager;

    }

    @Bean
    public static ConfigureRedisAction configureRedisAction() {
        return ConfigureRedisAction.NO_OP;
    }

//    @Bean("ehCacheManagerFactoryBean")
//    public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){
//        EhCacheManagerFactoryBean factoryBean = new EhCacheManagerFactoryBean();
//
//        factoryBean.setConfigLocation(new ClassPathResource("config/ehcache.xml"));
//        factoryBean.setShared(true);
//
//        return factoryBean;
//    }
//    /**
//     * ehcache 主要的管理器
//     * @param ehCacheManagerFactoryBean
//     * @return
//     */
//    @Bean("ehCacheCacheManager")
//    public EhCacheCacheManager ehCacheCacheManager(@Qualifier("ehCacheManagerFactoryBean") EhCacheManagerFactoryBean ehCacheManagerFactoryBean){
//        return new EhCacheCacheManager(ehCacheManagerFactoryBean.getObject());
//    }
//
//
//
//
//    @ConditionalOnMissingBean
//    @Bean
//    public EhCacheBasedUserCache ehCacheBasedUserCache(EhCacheManagerFactoryBean cacheManagerFactoryBean){
//        EhCacheBasedUserCache ehCacheBasedUserCache = new EhCacheBasedUserCache();
//        ehCacheBasedUserCache.setCache(cacheManagerFactoryBean.getObject().getCache("security_cache"));
//        return ehCacheBasedUserCache;
//    }
}
